<?php

namespace App\Http\Controllers\Admin\RoomManager;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class SoundEffectController extends BaseController {

    /**
     * @api {post} /room/sound-effect 房间音效-创建
     * @apiVersion 0.1.0
     * @apiName 房间管理-房间音效-创建
     * @apiGroup 房间管理
     *
     * @apiParam {string} name 名称 必传
     * @apiParam {string} icon 小图标 必传
     * @apiParam {string} icon_select 小图标，选中 必传
     * @apiParam {string} info 简介 必传
     * @apiParam {string} room_type 可用房间类型 必传
     * @apiParam {string} config 配置 必传
     * @apiParam {number} rank_id 最低房主等级 可选
     * @apiParam {number} status 状态 1显示0隐藏 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function create(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
            'name' => 'required|string|max:255',
            'icon' => 'required|string|active_url',
            'icon_select' => 'required|string|active_url',
            'info' => 'required|string|max:255',
            'room_type' => 'required|string',
            'config' => 'required|string',
            'rank_id' => 'filled|numeric|min:0|max:2100000000',
            'status' => 'required|numeric|in:0,1',
            ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $params = $request->input();
        if (isset($params['room_type'])) {
            $roomTypeIdArr = explode(",", $params['room_type']);
            $roomTypeList = app("model_room_type")->whereIn("id", $roomTypeIdArr)->select(["id"])->get();
            $totalRoomTypeIdList = empty($roomTypeList) ? [] : array_column($roomTypeList->toArray(), "id");
            if (count($roomTypeIdArr) != count($totalRoomTypeIdList)) {
                throw new BasicException(10001, '存在不合法的room_type！');
            }
        }

        $res = app('repo_sound_effect');
        $params['add_time'] = time();

        $rows = $res->addSoundEffect($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {delete} /room/sound-effect 房间音效-删除
     * @apiVersion 0.1.0
     * @apiName 房间管理-房间音效-删除
     * @apiGroup 房间管理
     *
     * @apiParam {string} ids                       字符串分割的id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function del(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'ids' => 'required|string',
            ]);

        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $idsStr = $request->input("ids");
        $idsArr = explode("," , $idsStr);

        $rows = app('repo_sound_effect')->delSoundEffect($idsArr);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /room/sound-effect 房间音效-修改
     * @apiVersion 0.1.0
     * @apiName 房间管理-房间音效-修改
     * @apiGroup 房间管理
     *
     *
     * @apiParam {int} id 必传
     * @apiParam {string} name 名称 必传
     * @apiParam {string} icon 小图标 必传
     * @apiParam {string} icon_select 小图标，选中 必传
     * @apiParam {string} info 简介 必传
     * @apiParam {string} room_type 可用房间类型 必传
     * @apiParam {string} config 配置 必传
     * @apiParam {number} rank_id 最低房主等级 可选
     * @apiParam {number} status 状态 1显示0隐藏 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function update(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
            'id' => 'required|integer',
            'name' => 'required|string|max:255',
            'icon' => 'required|string|active_url',
            'icon_select' => 'required|string|active_url',
            'info' => 'required|string|max:255',
            'room_type' => 'required|string',
            'config' => 'required|string',
            'rank_id' => 'filled|numeric|min:0|max:2100000000',
            'status' => 'required|numeric|in:0,1',
        ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $res = app('repo_sound_effect');

        $params = $request->input();
        $id = $params['id'];

        $rows = $res->saveSoundEffect(["id" => $id], $params);
        if (!$rows) {
            throw new BasicException(10001, "更新失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /room/sound-effect 房间音效-列表
     * @apiVersion 0.1.0
     * @apiName 房间管理-房间音效-列表
     * @apiGroup 房间管理
     *
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
    "code": 200,
    "data": {
        "list": [
            {
                "id(序号)": 3,
                "name(名称)": "动感音质",
                "info(简介)": "适合游戏、开黑",
                "room_type(可用房间类型)": "18,20,26,30",
                "room_type_name(可用房间类型名称)": "女神,娱乐,电台,男神",
                "rank_id(最低房主等级)": 10,
                "config(配置)": "{\n\"codecID\":6,\n\"bitrate\":48,\n\"channel\":1,\n\"HeadphoneAEC\":0,\n\"AGC\":1,\n\"AEC\":1,\n\"ANS\":1,\n\"ANSMode\":1,\n\"scenario\":1\n}",
                "status(状态 1显示0隐藏)": 1,
                "icon(小图标)": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5f38f2de1bb52.png",
                "icon_select(小图标，选中)": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5f38f2e33bf4e.png"
            }
        ],
        "meta": {
                "page": 1,
                "limit": 10,
                "next": null,
                "total": 1
            }
        }
     }
     *
     *
     */
    public function list(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $list = app('repo_sound_effect')->getSoundEffectQueryList($request->input());

        return Response::sendList($list);
    }


}
